import React, { useEffect } from 'react'
import { Route, Switch, Redirect }  from 'react-router-dom'
import Login from '../views/login/login'
import Index from '../views/index' 
import { connect } from 'react-redux'
import { routerAction } from '../redux/actionCreator/router'

const routes = [
  { path: '/login', title: '登录', component: Login },
  { path: '/', title: '首页', component: Index }
]

function IndexRouter(props) {
  console.log(props)

  const { routerAction } = props
  useEffect(() => {
    routerAction()
  }, [routerAction])
// return
  return (
    <Switch>
      {
        props.user._id ? 
          routes.map(item => {
            return <Route path={item.path} component={item.component} key={item.path}></Route>
          })
        :
        <Route path='/login' component={Login}></Route>
      }
      <Redirect to='/login'></Redirect>
    </Switch>
  )
}

export default connect((state) => {
  return {
    user: state.userReducer,
    routes: state.RouterReducer
  }
}, {
  routerAction
})(IndexRouter)
